import ApiFetchers from "@/apis/api_fetchers"
import { useCallback, useEffect, useState } from "react"

const getWalletAccountList = ApiFetchers.getFinanceAccountList

export type WalletAccountType = FinanceAccountApiTypes.Fetcher.Response.record

export default function useWithdrawAccounts() {
    const [accounts, setAccounts] = useState<WalletAccountType[]>()
    const [loading, setLoading] = useState(false)
    const [error, setError] = useState<string | null>(null)

    const walletAccountList = useCallback(async () => {
        try {
            setLoading(true)
            const res = await getWalletAccountList()
            setLoading(false)

            setAccounts(res?.list || [])
        } catch (error) {
            setError(error instanceof Error ? error.message : "get wallet account list failed")
        } finally {
            setLoading(false)
        }
    }, [])

    // 组件挂载时自动加载数据
    useEffect(() => {
        walletAccountList()
        // eslint-disable-next-line react-hooks/exhaustive-deps
    }, [])

    return {
        data: accounts,
        loading: loading,
        error: error,
        refresh: walletAccountList,
    }
}
